package com.jsu.clubmanagementsystem.controller.club;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jsu.clubmanagementsystem.common.BaseResponse;
import com.jsu.clubmanagementsystem.common.ErrorCode;
import com.jsu.clubmanagementsystem.common.PageQueryRequest;
import com.jsu.clubmanagementsystem.common.ResultUtils;
import com.jsu.clubmanagementsystem.constant.UserConstant;
import com.jsu.clubmanagementsystem.exception.BusinessException;
import com.jsu.clubmanagementsystem.model.dto.clubannouncement.ClubAnnouncementAddRequest;
import com.jsu.clubmanagementsystem.model.dto.clubannouncement.ClubAnnouncementQueryRequest;
import com.jsu.clubmanagementsystem.model.entity.ClubAnnouncement;
import com.jsu.clubmanagementsystem.model.entity.User;
import com.jsu.clubmanagementsystem.model.vo.clubAnnouncement.ClubAnnouncementVO;
import com.jsu.clubmanagementsystem.model.vo.clubAnnouncement.ListAnnouncementVO;
import com.jsu.clubmanagementsystem.service.club.ClubAnnouncementService;
import com.jsu.clubmanagementsystem.service.club.ClubMembershipService;
import com.jsu.clubmanagementsystem.service.club.ClubService;
import com.jsu.clubmanagementsystem.service.user.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/club/announcement")
public class ClubAnnouncementController {
    @Resource
    private ClubAnnouncementService announcementService;

    @Resource
    private UserService userService;

    @Resource
    private ClubService clubService;

    @Resource
    private ClubMembershipService clubMembershipService;

    @PostMapping("/add")
    public BaseResponse<Long> addClubAnnouncement(@RequestBody ClubAnnouncementAddRequest addRequest, HttpServletRequest request) {
        if (addRequest == null) {
            throw new BusinessException(ErrorCode.PARAM_ERROR, "参数为空");
        }
        Long clubId = addRequest.getClubId();
        if (clubId == null) {
            throw new BusinessException(ErrorCode.PARAM_ERROR, "参数为空");
        }

        User loginUser = userService.getLoginUser(request);
        if (!loginUser.getUserRole().equals(UserConstant.USER_ROLE_ADMIN) &&
                !clubService.isManagerOfClub(loginUser.getId(), clubId)) {
            throw new BusinessException(ErrorCode.NO_AUTH, "无权限编辑社团公告");
        }

        ClubAnnouncement announcement = new ClubAnnouncement();
        BeanUtils.copyProperties(addRequest, announcement);
        return ResultUtils.success(clubService.updateAnnouncement(announcement));
    }

    @GetMapping("/get")
    public BaseResponse<ClubAnnouncementVO> getClubAnnouncementVoById(long announcementId, HttpServletRequest request) {
        ClubAnnouncement clubAnnouncement = announcementService.getById(announcementId);
        if (clubAnnouncement == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND, "目标公告不存在");
        }
        User loginUser = userService.getLoginUser(request);
        if (!userService.isAdmin(request) &&
                !clubMembershipService.isMemberOfClub(loginUser.getId(), clubAnnouncement.getClubId())) {
            throw new BusinessException(ErrorCode.NO_AUTH, "非该社团成员，无法查看公告");
        }
        return ResultUtils.success(announcementService.getAnnouncementVoById(announcementId));
    }

    /**
     * 查看历史社团公告 (包括已过时的
     *
     * @param queryRequest
     * @param request
     * @return
     */
    @PostMapping("list")
    public BaseResponse<Page<ClubAnnouncementVO>> listClubAnnouncement(@RequestBody ClubAnnouncementQueryRequest queryRequest, HttpServletRequest request) {
        if (queryRequest == null) {
            throw new BusinessException(ErrorCode.PARAM_ERROR, "参数为空");
        }
        Long clubId = queryRequest.getClubId();
        if (clubId != null && !clubService.isExist(clubId)) {
            throw new BusinessException(ErrorCode.NOT_FOUND, "社团不存在");
        }
        User loginUser = userService.getLoginUser(request);
        if (!userService.isAdmin(request) &&
                !clubMembershipService.isMemberOfClub(loginUser.getId(), clubId)) {
            throw new BusinessException(ErrorCode.NO_AUTH, "非该社团成员，无法查看公告");
        }

        int current = queryRequest.getCurrent();
        int pageSize = queryRequest.getPageSize();
        List<ClubAnnouncement> announcementList = announcementService.getAllLogicAnnouncements(queryRequest);
        Page<ClubAnnouncement> page = new Page<>(current, pageSize);
        page.setRecords(announcementList);
        Page<ClubAnnouncementVO> voPage = announcementService.getAnnouncementVoPage(page);
        return ResultUtils.success(voPage);
    }

    /**
     * 获取所有公告
     */
    @PostMapping("/list/all")
    public BaseResponse<Page<ListAnnouncementVO>> getAllClubAnnouncement(@RequestBody PageQueryRequest page) {
        return ResultUtils.success(announcementService.getAllAnnouncements(page));
    }

    /**
     * 删除社团公告(管理员)
     */
    @DeleteMapping("/delete")
    public BaseResponse<Boolean> deleteClubAnnouncement(@RequestParam List<Long> announcementIds){
        return ResultUtils.success(announcementService.deleteAnnouncements(announcementIds));
    }

    @PostMapping("/newadd")
    public BaseResponse<Boolean> addClubAnnouncement(@RequestBody ClubAnnouncement announcement) {
        return ResultUtils.success(announcementService.addClubAnnouncement(announcement));
    }

    @PostMapping("/update")
    public BaseResponse<Boolean> updateClubAnnouncement(@RequestBody ClubAnnouncement announcement){
        if(announcementService.getById(announcement.getId())==null){
            throw new BusinessException(ErrorCode.NOT_FOUND,"公告不存在");
        }
        return ResultUtils.success(announcementService.updateById(announcement));
    }
}
